﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;

using OctoOS;
using OctoOS.UI;
using System.Web.UI.WebControls;

public partial class __admin_settings : OOSPage
{
    private Resource _resource;
    private Currency[] _currencies;

    protected void Page_Init(object sender, EventArgs e)
    {
#warning admin
        //OOSRequest.Current.EnsureModerator();

        _resource = DataSource.ClassifiedResources_Get(
                OOSRequest.Current.ResourceID,
                true,
                out _currencies);

        foreach (var currency in _currencies)
        {
            Currency.Items.Add(
                new ListItem(currency.Code, currency.ID.ToString()));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        PageTitle = Resources.Admin.Settings_PageTitle;
        AddBreadcrumb(string.Empty, "/");
        AddBreadcrumb("Admin Page", Urls.Classifieds.GetAdminDefault());


        if (_resource != null)
        {
            if (!IsPostBack)
            {
                AnonAds.Checked = _resource.AllowAnonAds;
                Comments.SelectedValue = _resource.Comments.ToString();
                AutoExpAnonDays.Value = _resource.AutoExpAnonDays;
                AutoExpNonAnonDays.Value = _resource.AutoExpNonAnonDays;
                RenewAds.Checked = _resource.RenewAds;
                Latitude.Value = _resource.Latitude;
                Longitude.Value = _resource.Longitude;
                Currency.SelectedValue = _resource.Currency.ToString();
                AdApprove.Checked = _resource.AdApprove;
                AdminMail.Text = _resource.AdminMail;
            }
            if (_resource.ExistsMasterResource)
            {
                AnonAds.Enabled = false;
                Comments.Enabled = false;
                AutoExpAnonDays.Enabled = false;
                AutoExpNonAnonDays.Enabled = false;
                RenewAds.Enabled = false;
                Currency.Enabled = false;
                AdApprove.Enabled = false;
            }
        }
    }

    protected void OnSubmitClick(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (!_resource.ExistsMasterResource)
            {
                DataSource.ClassifiedResources_Save(
                 OOSRequest.Current.ResourceID,
                 Double.IsNaN(Latitude.Value) ? 0 : Latitude.Value,
                 Double.IsNaN(Longitude.Value) ? 0 : Longitude.Value,
                 AnonAds.Checked,
                 Convert.ToByte(Comments.SelectedValue),
                 Convert.ToInt16(AutoExpNonAnonDays.Value),
                 Convert.ToInt16(AutoExpAnonDays.Value),
                 RenewAds.Checked,
                 AdApprove.Checked,
                 Convert.ToByte(Currency.SelectedValue),
                 AdminMail.TrimmedText);
            }
            else
            {
                DataSource.ClassifiedResources_Update(
                 OOSRequest.Current.ResourceID,
                 Double.IsNaN(Latitude.Value) ? 0 : Latitude.Value,
                 Double.IsNaN(Longitude.Value) ? 0 : Longitude.Value,
                 AdminMail.TrimmedText);
            }
            
            Response.Redirect(Urls.Classifieds.GetAdminDefault());
        }
    }
}
